package com.visionblue.business.ship.page.register.config

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.visionblue.business.ship.base.BaseViewModel
import com.visionblue.business.ship.model.repository.db.MeasureMenuItem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @author: xlh
 * @date: 2020-07-21 4:45 PM
 * @desc:
 */
class MenuConfigViewModel(private val repository: MenuConfigRepository) : BaseViewModel() {

    val inMenuItemsLiveData = MutableLiveData<List<MeasureMenuItem>>()
    val outMenuItemsLiveData = MutableLiveData<List<MeasureMenuItem>>()

    fun getMenuItems() {
        viewModelScope.launch {
            val menuItems = repository.getMenuItems()
//            menuItems.takeIf { it.isEmpty() }?.run {
//                repository.insert(
//                    arrayOf(
//                        MeasureMenuItem(WATER_VOLUME_NAME, "生活污水管"),
//                        MeasureMenuItem(OIL_VOLUME_NAME, "含油污水管"),
//                        MeasureMenuItem(WEIGHT_NAME, null),
//                        MeasureMenuItem(WATER_VOLUME_NAME, "生活污水管", isOutput = true),
//                        MeasureMenuItem(OIL_VOLUME_NAME, "含油污水管", isOutput = true),
//                        MeasureMenuItem(WEIGHT_NAME, null, isOutput = true)
//                    )
//                )
//            }

            val groupBy = menuItems.groupBy { it.isOutput }
            inMenuItemsLiveData.value = groupBy[false]
            outMenuItemsLiveData.value = groupBy[true]
        }
    }

    fun updateMenuItem(measureMenuItem: MeasureMenuItem) {
        viewModelScope.launch {
            repository.update(measureMenuItem)
        }
    }

}

const val WATER_VOLUME_NAME = "电磁流量计"
const val WATER_VOLUME_NAME2 = "电磁流量计."
const val OIL_VOLUME_NAME = "楔形流量计"
const val WEIGHT_NAME = "称重地磅"
const val ULTRASONIC_NAME = "超声波流量计"